﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nonocast.Scheduling {
	public class DayRange : IEnumerable<DateTime> {
		public DateTime StartDate { get; internal set; }
		public DateTime ExpirationDate { get; internal set; }

		public IEnumerator<DateTime> GetEnumerator() {
			int dayCount = (int)((ExpirationDate.Date - StartDate.Date).TotalDays);
			for (int i = 0; i < dayCount; ++i) {
				yield return StartDate.AddDays(i).Date;
			}
		}

		IEnumerator IEnumerable.GetEnumerator() {
			return this.GetEnumerator();
		}
	}


	public static class DayRangeExtension {
		public static DayRange FromToday(this DayRange self) {
			self.StartDate = DateTime.Now.Date;
			return self;
		}

		public static DayRange From(this DayRange self, DateTime date) {
			self.StartDate = date.Date;
			return self;
		}

		public static DayRange From(this DayRange self, string date) {
			self.StartDate = DateTime.Parse(date).Date;
			return self;
		}

		public static DayRange Until(this DayRange self, int last) {
			if (last < 0) throw new ArgumentOutOfRangeException();
			self.ExpirationDate = self.StartDate.AddDays(last);
			return self;
		}

		public static DayRange Until(this DayRange self, string date) {
			return self.Until(DateTime.Parse(date));
		}

		public static DayRange Until(this DayRange self, DateTime date) {
			if (date.Date < self.StartDate.Date) throw new ArgumentOutOfRangeException();
			self.ExpirationDate = self.StartDate.AddDays((date.Date - self.StartDate.Date).TotalDays);
			return self;
		}

		public static DayRange UntilWholeWeek(this DayRange self) {
			self.ExpirationDate = self.StartDate.AddDays(1 + 7);
			return self;
		}
	}
}
